package cn.ycl.auth.biz.usercenter.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.ycl.auth.biz.usercenter.service.UserRestApiService;
import cn.ycl.entity.UserInfoVO;
import cn.ycl.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author yuanchangliang
 * @date 2022-06-30 10:22
 **/
@Service
public class UserRestApiServiceImpl implements UserRestApiService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public UserInfoVO getUserInfo() {
        UserInfoVO userInfoVO = null;
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != requestAttributes) {
            HttpServletRequest request = requestAttributes.getRequest();
            String header = request.getHeader("token");
            if (StringUtils.isNotEmpty(header)) {
                String info = (String) redisUtil.get(header);
                //通过以下方式才能转化为UserInfoVO,直接转换报错
                JSONObject entries = JSONUtil.parseObj(info);
                userInfoVO = JSONUtil.toBean(entries, UserInfoVO.class);
            }
        }
        return userInfoVO;
    }
}
